import http from "@/utils/http";
import { FORM_DATA_AXIOS_CONFIG } from "../constants";

/**
 * 获得角色列表
 * @param {number} page
 * @param {number} size
 * @param {string} keyword
 * @returns
 */
export function getRolesData(page: number, size: number, keyword: string) {
  const url = `/Role/listrole/?page=${page}&page_size=${size}&search=${keyword}`;
  return http.get(url);
}

/**
 * 修改该ID角色信息
 * @param {*} param0
 * @returns
 */
export function updateRole({ id, rolename, remark, roletype }) {
  const params = new FormData();
  params.append("rolename", rolename);
  params.append("remark", remark);
  params.append("roletype", roletype);

  const url = `/Role/${id}/updateroleid/`;
  return http.post(url, params, FORM_DATA_AXIOS_CONFIG);
}

/**
 * 新增角色
 * @param {*} param
 * @returns
 */
export function insertRole({ rolename, remark, roletype }) {
  const params = new FormData();
  params.append("rolename", rolename);
  params.append("remark", remark);
  params.append("roletype", roletype);

  const url = `/Role/createrole/`;
  return http.post(url, params, FORM_DATA_AXIOS_CONFIG);
}

/**
 * 删除该ID角色
 * @param {string} id
 * @returns
 */
export function deleteRole(id: string) {
  const url = `/Role/${id}/delrole/`;
  return http.get(url);
}

/**
 * 获得未分配该规则的所有用户
 * @param {string} id
 */
export function getNouserByRole(id: string) {
  const url = `/Role/${id}/shownouser2/`;
  return http.get(url);
}

/**
 * 获得已分配该规则的所有用户
 * @param {string} id
 */
export function getUserByRole(id: string) {
  const url = `/Role/${id}/showuser2/`;
  return http.get(url);
}

/**
 * 对该用户移除角色
 * @param {string} uid
 * @param {string} rid
 */
export function removeRoleFromUser(uid: string, rid: string) {
  const params = new FormData();
  params.append("roleid", rid);
  params.append("empid", uid);

  const url = "/Role/unmanningrole/";
  return http.post(url, params, FORM_DATA_AXIOS_CONFIG);
}

/**
 * 对该用户添加角色
 * @param {string} uid
 * @param {string} rid
 */
export function addRoleFromUser(uid: string, rid: string) {
  const params = new FormData();
  params.append("roleid", rid);
  params.append("empid", uid);

  const url = `/Role/manningrole/`;
  return http.post(url, params, FORM_DATA_AXIOS_CONFIG);
}
